﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.PointCloud.CUiPOCPointCloudDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Options;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.PointCloud;

public class CUiPOCPointCloudDlg : TxForm, ITxFormAutoTest
{
  private const string LAST_BROWSE_LOCATION = "LAST_BROWSE_LOCATION";
  private string m_lastBrowseLocation;
  private bool m_canClose = true;
  private IContainer components;
  private Button m_buttonCreate;
  private Button m_buttonCancel;
  private Button m_buttonBrowse;
  private Label label1;
  private Label label2;
  private TextBox textBoxPointCloudName;
  private TextBox textBoxPointCloudFile;

  public CUiPOCPointCloudDlg()
  {
    this.AutoTestRunning = false;
    this.InitializeComponent();
    this.LoadSettings();
    this.InitControls();
    this.EnableCreation();
  }

  private void InitControls()
  {
    this.textBoxPointCloudName.Text = TxApplication.ActiveDocument.PhysicalRoot.GetUniqueDisplayName(PointCloudStringTable.POINT_CLOUD_NAME_PREFIX);
    this.textBoxPointCloudFile.Text = this.StartPODTextBoxText();
  }

  private string StartPODTextBoxText() => PointCloudStringTable.BROWSE_FILE;

  private void LoadSettings()
  {
    this.m_lastBrowseLocation = TxOptionsEx.PointCloudRoot;
    object obj;
    TxFormSettings.RestoreFormValue((Form) this, "LAST_BROWSE_LOCATION", ref obj);
    if (obj != null)
    {
      try
      {
        this.m_lastBrowseLocation = Convert.ToString(obj);
      }
      catch (Exception ex)
      {
        TxApplication.LogWriter.WriteExceptionLine(ex);
      }
    }
    if (this.IsSubfolder(TxOptionsEx.PointCloudRoot, this.m_lastBrowseLocation))
      return;
    this.m_lastBrowseLocation = TxOptionsEx.PointCloudRoot;
  }

  private void SaveSettings()
  {
    TxFormSettings.StoreFormValue((Form) this, "LAST_BROWSE_LOCATION", (object) this.m_lastBrowseLocation);
  }

  private void EnableCreation()
  {
    string text = this.textBoxPointCloudFile.Text;
    bool flag = this.textBoxPointCloudName.Text != string.Empty;
    if (flag)
      flag = this.ValidPointCloudFile(text) && this.ValidateFileUnderPointCloudRoot(text);
    this.m_buttonCreate.Enabled = flag;
  }

  private bool ValidPointCloudRoot()
  {
    bool flag = false;
    if (Path.IsPathRooted(TxOptionsEx.PointCloudRoot))
      flag = new DirectoryInfo(TxOptionsEx.PointCloudRoot).Exists;
    return flag;
  }

  private bool ValidPointCloudFile(string fileName)
  {
    bool flag = false;
    if (fileName != string.Empty && Path.IsPathRooted(fileName))
      flag = new FileInfo(fileName).Exists;
    return flag;
  }

  private bool ValidateFileUnderPointCloudRoot(string fileName)
  {
    bool flag = false;
    if (fileName != string.Empty)
      flag = this.IsSubfolder(TxOptionsEx.PointCloudRoot, new FileInfo(fileName).Directory.FullName);
    return flag;
  }

  private bool IsSubfolder(string rootFolder, string subFolder)
  {
    rootFolder = rootFolder.TrimBackSlash();
    bool flag = false;
    if (Path.IsPathRooted(rootFolder) && Path.IsPathRooted(subFolder))
    {
      DirectoryInfo directoryInfo = new DirectoryInfo(rootFolder);
      flag = new DirectoryInfo(subFolder).FullName.ToLower().StartsWith(directoryInfo.FullName.ToLower());
    }
    return flag;
  }

  private string GetRelativePath(string rootFolder, string fullPath)
  {
    string relativePath = fullPath;
    if (this.IsSubfolder(rootFolder, fullPath))
      relativePath = relativePath.Remove(0, rootFolder.Length);
    return relativePath;
  }

  private void m_buttonCreate_Click(object sender, EventArgs e)
  {
    string text = this.textBoxPointCloudName.Text;
    TxPhysicalRootEx txPhysicalRootEx = new TxPhysicalRootEx();
    string pointCloudRoot = TxOptionsEx.PointCloudRoot;
    string relativePath = this.GetRelativePath(pointCloudRoot, this.textBoxPointCloudFile.Text);
    TxPointCloudCreationDataEx cloudCreationDataEx = new TxPointCloudCreationDataEx(text, this.textBoxPointCloudFile.Text, relativePath);
    try
    {
      if (!txPhysicalRootEx.DoesPointCloudExist(relativePath, pointCloudRoot))
      {
        txPhysicalRootEx.CreatePointCloud(cloudCreationDataEx);
        ArrayList allParameters = TxErrorStack.GetAllParameters("API_GRAPHICS_POINTCLOUD_FILE_WRONG_TYPE");
        bool flag = false;
        for (int index = allParameters.Count - 1; index >= 0; --index)
        {
          if (!flag)
          {
            if (allParameters[index] is IList<string> stringList && stringList[0].Equals(this.textBoxPointCloudFile.Text))
            {
              flag = true;
              int num = (int) this.ShowTextMessage(PointCloudStringTable.POINT_CLOUD_FILE_WRONG_TYPE, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
              this.m_canClose = false;
            }
          }
          else
            break;
        }
      }
      else
      {
        int num = (int) this.ShowTextMessage(PointCloudStringTable.POINT_CLOUD_FILES_EXIST, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        this.m_canClose = false;
      }
    }
    catch (TxPointCloudCreationFailedException ex)
    {
      int num = (int) this.ShowTextMessage(PointCloudStringTable.POINT_CLOUD_FAILED_TO_CREATE, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      this.m_canClose = false;
    }
    catch (TxPointCloudNotSupportedException ex)
    {
      int num = (int) this.ShowTextMessage(PointCloudStringTable.POINT_CLOUD_NOT_SUPPORTED, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      this.m_canClose = false;
    }
    TxApplication.RefreshDisplay();
    this.SaveSettings();
  }

  private void CUiPOCPointCloudDlg_FormClosing(object sender, FormClosingEventArgs e)
  {
    if (this.m_canClose)
      return;
    e.Cancel = true;
    this.m_canClose = true;
  }

  private void m_buttonBrowse_Click(object sender, EventArgs e)
  {
    string path = this.textBoxPointCloudFile.Text;
    if (!this.ValidPointCloudRoot())
    {
      int num1 = (int) this.ShowTextMessage(PointCloudStringTable.POINT_CLOUD_ROOT_DONT_EXISTS, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    else
    {
      int num2 = 1;
      TxPhysicalRootEx txPhysicalRootEx = new TxPhysicalRootEx();
      if (TxGraphicViewerEx.The.ActiveSectionVolumes > num2)
      {
        int num3 = (int) this.ShowTextMessage(PointCloudStringTable.POINT_CLOUD_MORE_THAN_ONE_SECTION_VOLUME, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      }
      using (OpenFileDialog openFileDialog = new OpenFileDialog())
      {
        openFileDialog.Filter = PointCloudStringTable.OPEN_DIALOG_FILE_FILTER;
        openFileDialog.Title = PointCloudStringTable.POINT_CLOUD_SELECT_FILE_TITLE;
        openFileDialog.InitialDirectory = this.m_lastBrowseLocation;
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
          if (this.ValidateFileUnderPointCloudRoot(openFileDialog.FileName))
          {
            path = openFileDialog.FileName;
            this.m_lastBrowseLocation = Path.GetDirectoryName(path);
          }
          else
          {
            int num4 = (int) this.ShowTextMessage(PointCloudStringTable.SELECTED_FILE_IS_NOT_IN_POINT_CLOUD_ROOT, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            path = this.StartPODTextBoxText();
          }
        }
      }
    }
    this.textBoxPointCloudFile.Text = path;
  }

  private void textBoxPointCloudFile_TextChanged(object sender, EventArgs e)
  {
    string text = this.textBoxPointCloudFile.Text;
    if (this.ValidPointCloudFile(text) && !this.ValidateFileUnderPointCloudRoot(text))
    {
      int num = (int) this.ShowTextMessage(PointCloudStringTable.SELECTED_FILE_IS_NOT_IN_POINT_CLOUD_ROOT, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    this.EnableCreation();
  }

  private void textBoxPointCloudName_TextChanged(object sender, EventArgs e)
  {
    this.EnableCreation();
  }

  private DialogResult ShowTextMessage(
    string text,
    string caption,
    MessageBoxButtons buttons,
    MessageBoxIcon icon)
  {
    return this.AutoTestRunning ? DialogResult.None : TxMessageBox.Show(text, caption, buttons, icon);
  }

  public bool AutoTestRunning { get; set; }

  public bool SetField(string fieldName, object fieldValue)
  {
    if (fieldName == "PODFilePath")
      this.textBoxPointCloudFile.Text = fieldValue.ToString();
    return false;
  }

  public bool PressButton(string buttonName)
  {
    bool flag = false;
    switch (buttonName)
    {
      case "Insert":
        this.m_buttonCreate.PerformClick();
        flag = true;
        break;
      case "Close":
        this.m_buttonCancel.PerformClick();
        flag = true;
        break;
    }
    return flag;
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiPOCPointCloudDlg));
    this.m_buttonCreate = new Button();
    this.m_buttonCancel = new Button();
    this.m_buttonBrowse = new Button();
    this.label1 = new Label();
    this.label2 = new Label();
    this.textBoxPointCloudName = new TextBox();
    this.textBoxPointCloudFile = new TextBox();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_buttonCreate, "m_buttonCreate");
    this.m_buttonCreate.DialogResult = DialogResult.OK;
    this.m_buttonCreate.Name = "m_buttonCreate";
    this.m_buttonCreate.UseVisualStyleBackColor = true;
    this.m_buttonCreate.Click += new EventHandler(this.m_buttonCreate_Click);
    componentResourceManager.ApplyResources((object) this.m_buttonCancel, "m_buttonCancel");
    this.m_buttonCancel.DialogResult = DialogResult.Cancel;
    this.m_buttonCancel.Name = "m_buttonCancel";
    this.m_buttonCancel.UseVisualStyleBackColor = true;
    componentResourceManager.ApplyResources((object) this.m_buttonBrowse, "m_buttonBrowse");
    this.m_buttonBrowse.Name = "m_buttonBrowse";
    this.m_buttonBrowse.UseVisualStyleBackColor = true;
    this.m_buttonBrowse.Click += new EventHandler(this.m_buttonBrowse_Click);
    componentResourceManager.ApplyResources((object) this.label1, "label1");
    this.label1.Name = "label1";
    componentResourceManager.ApplyResources((object) this.label2, "label2");
    this.label2.Name = "label2";
    componentResourceManager.ApplyResources((object) this.textBoxPointCloudName, "textBoxPointCloudName");
    this.textBoxPointCloudName.Name = "textBoxPointCloudName";
    this.textBoxPointCloudName.TextChanged += new EventHandler(this.textBoxPointCloudName_TextChanged);
    componentResourceManager.ApplyResources((object) this.textBoxPointCloudFile, "textBoxPointCloudFile");
    this.textBoxPointCloudFile.Name = "textBoxPointCloudFile";
    this.textBoxPointCloudFile.TextChanged += new EventHandler(this.textBoxPointCloudFile_TextChanged);
    ((Form) this).AcceptButton = (IButtonControl) this.m_buttonCreate;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Form) this).CancelButton = (IButtonControl) this.m_buttonCancel;
    ((Control) this).Controls.Add((Control) this.textBoxPointCloudFile);
    ((Control) this).Controls.Add((Control) this.textBoxPointCloudName);
    ((Control) this).Controls.Add((Control) this.label2);
    ((Control) this).Controls.Add((Control) this.label1);
    ((Control) this).Controls.Add((Control) this.m_buttonBrowse);
    ((Control) this).Controls.Add((Control) this.m_buttonCancel);
    ((Control) this).Controls.Add((Control) this.m_buttonCreate);
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiPOCPointCloudDlg);
    this.SemiModal = false;
    this.ShouldCloseOnDocumentUnloading = true;
    ((Form) this).FormClosing += new FormClosingEventHandler(this.CUiPOCPointCloudDlg_FormClosing);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
